import page from '../../databus/page';
import positionsUpdateTpl from '../../views/positions-update.art';
import positionsUpdateFormTpl from '../../views/positions-update-form.art';

import {
  positionsUpdateModel
} from '../../models/positions';
import http from '../../utils/http';

//添加用户
export const updatePosition = () => {

  //渲染模板 继users.art 渲染之后再渲染
  $('#position-list-box').after(positionsUpdateTpl());

  const _save = async () => {

    try {
      let result = await positionsUpdateModel();

      if (result.ret) {
        page.setCurrentPage(1);
        //获取用户列表数据
        //告知list页面要重新渲染
        $('body').trigger('addPosition');
      }

      //单击关闭模态框
      $('#positions-close-update').click();
    } catch (err) {
      console.log(err);
    }
  }

  //点击模态框保存，提交表单
  $('#positions-save-update').off('click').on('click', _save);
}

//添加用户
export const fillPositionsUpdateTpl = async (id) => {
  let {
    result
  } = await http({
    url: '/api/positions/listone',
    type: 'post',
    data: {
      id
    }
  })

  //渲染模板 继users.art 渲染之后再渲染
  $('#position-from-update').html(positionsUpdateFormTpl({
    data: {
      ...result
    }
  }));
}